extends Node2D

@onready var player = %Player
@onready var flags = $"../Flags"
const STAGE_CLEAR = preload("res://asserts/music/stage_clear.ogg")
const GAME_OVER = preload("res://asserts/music/game_over.ogg")



func _life_up():
	GlobalGameInfo.player_life += 1

func subtract_life():
	GlobalGameInfo.player_life = max(0,GlobalGameInfo.player_life -1)

func life_is_zero():
	return GlobalGameInfo.player_life == 0

func _on_area_2d_body_entered(_body):
	player._on_flags_player_stop()
	flags._on_finish_level()
	%BGM.stream = STAGE_CLEAR
	%BGM.play()

func _on_player_player_die():
	print("player_life %s" % GlobalGameInfo.player_life)
	if life_is_zero():
		game_over()
		get_tree().change_scene_to_file("res://sence/game_over.tscn")
		return
	print("Challenge  again!")
	subtract_life()
	await get_tree().create_timer(3).timeout
	GlobalGameInfo.reload_sence()


func _on_kill_zone_body_entered(_body):
	print("player_life %s" % GlobalGameInfo.player_life)
	if life_is_zero():
		game_over()
		return
	print("Challenge  again!")
	subtract_life()
	GlobalGameInfo.reload_sence()

func game_over():
	%BGM.stream = GAME_OVER
	%BGM.play()
